package cn.kkcxy.blog.core.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * @BelongsProject: kkcxy-blog
 * @BelongsPackage: cn.kkcxy.blog.core.result
 * @Author: kkcxy
 * @CreateTime: 2024-09-11 22:50
 * @Description: 统一返回结果
 * @Version: 1.0
 */
@Data
@Schema(description = "统一返回结果实体类")
public class ResponseEntity<T> {

    /**
     * 响应编码 0为正常 -1为错误
     */
    @Schema(description = "响应编码 0为正常 -1为错误")
    private Integer code;

    @Schema(description = "响应信息")
    private String message;

    @Schema(description = "是否成功")
    private Boolean success;

    @Schema(description = "响应数据")
//    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public ResponseEntity() {
        this(0, "操作成功", true);
    }

    public ResponseEntity(Integer code, String message, Boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
    }

    public static <T> ResponseEntity<T> success() {
        return new ResponseEntity<T>();
    }

    public static <T> ResponseEntity<T> success(T data) {
        ResponseEntity<T> entity = new ResponseEntity<>();
        entity.setData(data);
        return entity;
    }

    public static <T> ResponseEntity<T> fail() {
        return new ResponseEntity<>(-1, "操作失败", false);
    }

    public static <T> ResponseEntity<T> fail(String message) {
        return new ResponseEntity<>(-1, message, false);
    }

}


